package com.epsoft.odin7cj.flow.example;

import java.io.Serializable;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * 启动流程
 * @author Administrator
 *
 */
public class C07StartFlow {

	public static void main1(String[] args) throws Exception {
		
		// 流程引擎
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		
		// 获取存储服务
		RepositoryService repService = engine.getRepositoryService();
		// 获取运行服务
		RuntimeService runService = engine.getRuntimeService();
		// 获取任务服务
		TaskService taskService = engine.getTaskService();
		
		/**
		DeploymentBuilder builder = repService.createDeployment();
		builder = builder.addClasspathResource("bpmn/c07-task-flow.bpmn");
		// 发布流程
		Deployment deployment = builder.deploy();
		// 获取流程定义
		ProcessDefinition pd = repService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
		// 启动流程实例
		ProcessInstance pi = runService.startProcessInstanceById(pd.getId());	
		*/
		
		String procInstaceId = "27505";
		
//		procInstaceId = pi.getId();
		
		List<Task> taskList = taskService.createTaskQuery().processInstanceId(procInstaceId).list();	
		
		for(Task tmpTask : taskList) {
			Execution execution = runService.createExecutionQuery().executionId(tmpTask.getExecutionId()).singleResult();
			if("TaskA".equals(tmpTask.getName())) {
				// 作用域在子执行流
				runService.setVariableLocal(execution.getId(), "TaskA_variable","TaskA 变量值");
			}
			else {
				// 作用域在父执行流
				runService.setVariable(execution.getId(), "TaskB_variable","TaskB 变量值");
			}
			System.out.println("\n TaskName:"+tmpTask.getName());
		}
		
		// 使两个任务都完成
		for(Task tmpTask : taskList) {
			taskService.complete(tmpTask.getId());
		}
				
		Task taskC = taskService.createTaskQuery().processInstanceId(procInstaceId).singleResult();	
		
		System.out.println("\n TaskA_variable:"+runService.getVariable(taskC.getExecutionId(), "TaskA_variable"));
		System.out.println("\n TaskB_variable:"+runService.getVariable(taskC.getExecutionId(), "TaskB_variable"));

	}
	
}

	
